package com.voja.mrvica2.listener;

import com.voja.mrvica2.Entry;
import com.voja.mrvica2.Index;
import com.voja.mrvica2.adapter.EntryAdapter;

import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;

public class ItemScrollListener implements OnScrollListener {

    Index activity;
    
    public ItemScrollListener(Index activity) {
        this.activity = activity;
    }
    
    public void onScrollStateChanged(AbsListView view, int scrollState) {
        // TODO Auto-generated method stub
    }
            
    @Override
    public void onScroll(AbsListView view, int first, int count, int total) {
        if ((first + count) == total && total > 0) {
            EntryAdapter adapter = (EntryAdapter)view.getAdapter();
            
            if (adapter != null && !EntryAdapter.loaded && !EntryAdapter.loading) {
                EntryAdapter.loading = true;
                Entry last = adapter.getItem(total - 1);
                if (last != null && !last.isLoading()) {
                    Entry e = new Entry();
                    e.setLoading(Index.context);
                    
                    adapter.add(e);
                    Index.page++;
                    activity.loadItems();
                }
            }
        }
    }
}
